import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:fluttertoast/fluttertoast.dart';

class DataBean {
  final String key;
  final String message;

  DataBean(this.key, this.message);

  static List<DataBean> datas = new List<DataBean>();

  //json转化data
  static List<DataBean> decodeData(String data) {
    datas.clear();
    var newData = json.decode(data);
    var results = newData['message'];
    if (results.length == 0) {
      Fluttertoast.showToast(
          msg: "没有查找到相似词，请重新输入",
          timeInSecForIos: 1,
          backgroundColor: Colors.deepPurpleAccent,
          textColor: Colors.black);
    }
    for (int i = 0; i < results.length; i++) {
      datas.add(forMap(results[i]));
    }
    return datas;
  }

  static DataBean forMap(result) {
    var key = result["key"];
    var value = result["value"];
    var c = "";
    List means = result["means"];
    if (means.length == 0) {
      Fluttertoast.showToast(
          msg: "没有查找到相似词，请重新输入",
          timeInSecForIos: 1,
          backgroundColor: Colors.deepPurpleAccent,
          textColor: Colors.black);
    }

    for (int i = 0; i < means.length; i++) {
      var mean = means[i];
      List mean0 = mean['means'];
      for (int i = 0; i < mean0.length; i++) {
        c = c + mean0[i] + " && ";
      }
    }
    return new DataBean(c, key);
  }
}
